package com.imu.purse.modules.base.service.impl;

import java.math.BigDecimal;
import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.imu.common.exception.ErrorDesc;
import com.imu.common.exception.RRException;
import com.imu.constant.Constant;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.base.dao.SysConfigMapper;
import com.imu.purse.modules.base.entity.SysConfigEntity;
import com.imu.purse.modules.base.service.SysConfigService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 私募明细
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-22 10:33:20
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class SysConfigServiceImpl extends BaseService<SysConfigEntity> implements SysConfigService 
{
	@Override
    @Transactional
    public int save(final SysConfigEntity entity)
	{
    	return super.insert(entity);
	}
	
	@Override
	@Transactional
    public int update(final SysConfigEntity tradeInfo) 
    {
    	return mapper.updateByPrimaryKeySelective(tradeInfo);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final SysConfigEntity tradeInfo)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(tradeInfo)))
    	{
        	len = this.insert(tradeInfo);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    public SysConfigEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }

    @Override
	@Transactional
    public BigDecimal priGrab(final BigDecimal count)
    {
    	String ret = redisTemplate.execute(SCRIPT, redisTemplate.getStringSerializer(), redisTemplate.getStringSerializer(), Collections.singletonList(Constant.REDIS_CFG_KEY), Constant.CFG_COWRY_COUNT_KEY, count.toString());
    	BigDecimal _ret = null;
    	if(null == ret || (_ret = new BigDecimal(ret)).longValue() == -1)
		{
			this.findByKey(Constant.CFG_COWRY_COUNT_KEY);
			ret = redisTemplate.execute(SCRIPT, redisTemplate.getStringSerializer(), redisTemplate.getStringSerializer(), Collections.singletonList(Constant.REDIS_CFG_KEY), Constant.CFG_COWRY_COUNT_KEY, count.toString());
		}
		if(null == ret || (_ret = new BigDecimal(ret)).longValue() == -1)
		{
			throw new RRException(ErrorDesc.PRIEQUITY_THAN.getMsg(), ErrorDesc.PRIEQUITY_THAN.getCode());
		}
		else if(-9 == _ret.longValue())
		{
			throw new RRException(ErrorDesc.PRIEQUITY_MORE.getMsg(), ErrorDesc.PRIEQUITY_MORE.getCode());
		}
		
		SysConfigEntity entity = new SysConfigEntity();
		entity.setKey(Constant.CFG_COWRY_COUNT_KEY);
		entity.setValue(ret);
		mapper.upByKey(entity);
		
		return new BigDecimal(ret);
    }
    
    public SysConfigEntity findByKey(final String key)
    {
    	if(StrUtil.isBlank(key)) return null;
    	
    	SysConfigEntity entity = null;
		HashOperations<String, String, String> operations = redisTemplate.opsForHash();
    	String info = operations.get(Constant.REDIS_CFG_KEY, key);
    	if(StrUtil.isBlank(info))
    	{
    		entity = mapper.queryByKey(key);
    		if(null == entity || StrUtil.isBlank(entity.getKey())) return null;
    		
    		operations.put(Constant.REDIS_CFG_KEY, entity.getKey(), JSON.toJSONString(entity));
    		return entity;
    	}
    	
    	return JSON.parseObject(info, SysConfigEntity.class);
    }
    
	@Autowired
	private SysConfigMapper mapper;
	
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final RedisScript<String> SCRIPT = new DefaultRedisScript<String>(Constant.PRI_COWRY_SCRIPT, String.class);
}
